package com.hand.demo.app.service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hand.demo.domain.entity.XzyExample;
import org.hzero.boot.imported.app.service.ValidatorHandler;
import org.hzero.boot.imported.infra.validator.annotation.ImportValidator;
import org.hzero.boot.imported.infra.validator.annotation.ImportValidators;
import org.springframework.beans.factory.annotation.Autowired;
import java.io.IOException;

// 继承ValidatorHandler实现自定义校验
@ImportValidators({
        @ImportValidator(templateCode = "CLIENT_IMPORT_YIGE")
})
public class XzyImportValidator extends ValidatorHandler {
    @Autowired
    private ObjectMapper objectMapper;

    @Override
    public boolean validate(String data) {
        XzyExample user;
        try {
            user = objectMapper.readValue(data, XzyExample.class);
        } catch (IOException e) {
            // 失败
            return false;
        }
        if(user.getName().equals("菜虚鲲")) {
           getContext().addErrorMsg("菜虚鲲为敏感数据!!!");
           return false;
        }

        // 成功
        return true;
    }
}